package com.zcf.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.template.LinkTemplate;

public class AppPush {

	// 定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
	// 软件logo图片
	// private static final String logoUrl =
	// "http://47.92.212.176:8080/jiuan/img/jiuan.png";

	private static String appId = "KoNY2d8LUU7ylCHWxBLjz6";

	private static String appKey = "f5GNGoSZaH9oWj1BtyKgM";

	private static String masterSecret = "uamBN6XI4s65UBbxIhBlX3";
	// 用户生成的个人个推唯一标识符
	//static String cid = "rMOT2QMSbc7NtC50h1s1e";
	static String host = "http://sdk.open.api.igexin.com/apiex.htm";
	private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
//	private static String url = "http://121.196.28.39/paotui/apk/index.html";

	/*
	 * public static void main(String[] args) throws IOException {
	 * 
	 * //配置返回每个⽤户返回⽤户状态，可选 System.setProperty("gexin_pushList_needDetails", "true");
	 * //配置返回每个别名及其对应cid的⽤户状态，可选
	 * System.setProperty("gexin_pushList_needAliasDetails", "true"); IGtPush push =
	 * new IGtPush(host, appKey, masterSecret); //通知透传模板 NotificationTemplate
	 * template = notificationTemplateDemo("标题", "123"); ListMessage message = new
	 * ListMessage(); message.setData(template); // 设置消息离线，并设置离线时间
	 * message.setOffline(true); // 离线有效时间，单位为毫秒，可选 message.setOfflineExpireTime(24
	 * * 1000 * 3600); // 配置推送⽬标 List<Target> targets = new ArrayList<Target>();
	 * Target target = new Target(); target.setAppId(appId);
	 * target.setClientId(cid); targets.add(target); // taskId⽤于在推送时去查找对应的message
	 * String taskId = push.getContentId(message); IPushResult ret =
	 * push.pushMessageToList(taskId, targets);
	 * System.out.println(ret.getResponse().toString()); }
	 */
	public static void main(String[] args) throws IOException {

		IGtPush push = new IGtPush(url, appKey, masterSecret);

		// 定义"点击链接打开通知模板"，并设置标题、内容、链接
		LinkTemplate template = new LinkTemplate();
		template.setAppId(appId);
		template.setAppkey(appKey);
		template.setTitle("小鸡外卖");
		template.setText("您有一个新的订单");
		template.setUrl("http://getui.com");

		List<String> appIds = new ArrayList<String>();
		appIds.add(appId);
		
		// 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
		AppMessage message = new AppMessage();
		message.setData(template);
		message.setAppIdList(appIds);
		message.setOffline(true);
		message.setOfflineExpireTime(1000 * 600);

		IPushResult ret = push.pushMessageToApp(message);
		System.out.println(ret.getResponse().toString());
	}

//	public static void main(String[] args) {
//		pushinfo("测试",cid);
//	}
	/**
	 * 推送消息
	 * 
	 * @param title   标题
	 * @param content 提示内容
	 * @param clist   cids 获取用户对应的clentsid
	 */
	public static void pushinfo(/* String title, String content, */ String cid/* List<String> clist */) {

 		IGtPush push = new IGtPush(host, appKey, masterSecret);
		// 通知透传模板
		System.setProperty("gexin_pushList_needDetails", "true");
		NotificationTemplate template = notificationTemplateDemo("小鸡外卖", "您有一个新的订单");
		ListMessage message = new ListMessage();
		message.setData(template);
		// 设置消息离线，并设置离线时间
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		List<Target> targets = new ArrayList<Target>();
		// for (int i = 0; i < clist.size(); i++) {
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(cid);
		targets.add(target);
		// }
		// taskId⽤于在推送时去查找对应的message
		String taskId = push.getContentId(message);
		IPushResult ret = push.pushMessageToList(taskId, targets);
		System.out.println(ret.getResponse().toString());


		// 透傳消息，不是通知欄顯示，而是在MessageReceiver收到
		TransmissionTemplate template2 = new TransmissionTemplate();
		template2.setAppId(appId);
		template2.setAppkey(appKey);
		template2.setTransmissionContent("您有一个新的订单");
		template2.setTransmissionType(0); //這個Type為int型，填寫1則自動啟動app

		ListMessage message2 = new ListMessage();
		message2.setData(template2); // 把透傳消息設置到單消息模版中
		message2.setOffline(true); // 是否運行離線發送
		message2.setOfflineExpireTime(24 * 3600 * 1000); // 離線消息時常

		// 設置推送目標，填入appid和clientId
		List<Target> targets2 = new ArrayList<Target>();
		Target target2 = new Target();
		target2.setAppId(appId);
		target2.setClientId(cid);
		targets2.add(target);

		String taskId2 = push.getContentId(message2);
		IPushResult ret2 = push.pushMessageToList(taskId2, targets);

	}

	public static void pushinfoxin(/* String title, String content, */ String cid/* List<String> clist */) {

		IGtPush push = new IGtPush(host, appKey, masterSecret);
		// 通知透传模板
		System.setProperty("gexin_pushList_needDetails", "true");
		NotificationTemplate template = notificationTemplateDemo("小鸡外卖", "小鸡外卖有新订单啦，请前去抢单！");

		ListMessage message = new ListMessage();
		message.setData(template);
		// 设置消息离线，并设置离线时间
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		List<Target> targets = new ArrayList<Target>();
		// for (int i = 0; i < clist.size(); i++) {
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(cid);
		targets.add(target);
		// }
		// taskId⽤于在推送时去查找对应的message

		String taskId = push.getContentId(message);
		IPushResult ret = push.pushMessageToList(taskId, targets);


		// 透傳消息，不是通知欄顯示，而是在MessageReceiver收到
		TransmissionTemplate template2 = new TransmissionTemplate();
		template2.setAppId(appId);
		template2.setAppkey(appKey);
		template2.setTransmissionContent("小鸡外卖有新订单啦，请前去抢单！");
		template2.setTransmissionType(0); //這個Type為int型，填寫1則自動啟動app

		ListMessage message2 = new ListMessage();
		message2.setData(template2); // 把透傳消息設置到單消息模版中
		message2.setOffline(true); // 是否運行離線發送
		message2.setOfflineExpireTime(24 * 3600 * 1000); // 離線消息時常

		// 設置推送目標，填入appid和clientId
		List<Target> targets2 = new ArrayList<Target>();
		Target target2 = new Target();
		target2.setAppId(appId);
		target2.setClientId(cid);
		targets2.add(target);

		String taskId2 = push.getContentId(message2);
		IPushResult ret2 = push.pushMessageToList(taskId2, targets);

	}
	public static void pushinfoding(/* String title, String content, */ String cid/* List<String> clist */) {

		IGtPush push = new IGtPush(host, appKey, masterSecret);
		// 通知透传模板
		System.setProperty("gexin_pushList_needDetails", "true");
		NotificationTemplate template = notificationTemplateDemo("小鸡外卖", "骑手已经接单，请稍等");
		ListMessage message = new ListMessage();
		message.setData(template);
		// 设置消息离线，并设置离线时间
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		List<Target> targets = new ArrayList<Target>();
		// for (int i = 0; i < clist.size(); i++) {
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(cid);
		targets.add(target);
		// }
		// taskId⽤于在推送时去查找对应的message
		String taskId = push.getContentId(message);
		IPushResult ret = push.pushMessageToList(taskId, targets);
		System.out.println(ret.getResponse().toString());

	}
	public static void pushinforiding(/* String title, String content, */ String cid/* List<String> clist */) {

		IGtPush push = new IGtPush(host, appKey, masterSecret);
		// 通知透传模板
		System.setProperty("gexin_pushList_needDetails", "true");
		NotificationTemplate template = notificationTemplateDemo("小鸡外卖", "商家已经接单，请稍等");
		ListMessage message = new ListMessage();
		message.setData(template);
		// 设置消息离线，并设置离线时间
		message.setOffline(true);
		// 离线有效时间，单位为毫秒，可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		List<Target> targets = new ArrayList<Target>();
		// for (int i = 0; i < clist.size(); i++) {
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(cid);
		targets.add(target);
		// }
		// taskId⽤于在推送时去查找对应的message
		String taskId = push.getContentId(message);
		IPushResult ret = push.pushMessageToList(taskId, targets);
		System.out.println(ret.getResponse().toString());


		// 透傳消息，不是通知欄顯示，而是在MessageReceiver收到
		TransmissionTemplate template2 = new TransmissionTemplate();
		template2.setAppId(appId);
		template2.setAppkey(appKey);
		template2.setTransmissionContent("商家已经接单，请稍等");
		template2.setTransmissionType(0); //這個Type為int型，填寫1則自動啟動app

		ListMessage message2 = new ListMessage();
		message2.setData(template2); // 把透傳消息設置到單消息模版中
		message2.setOffline(true); // 是否運行離線發送
		message2.setOfflineExpireTime(24 * 3600 * 1000); // 離線消息時常

		// 設置推送目標，填入appid和clientId
		List<Target> targets2 = new ArrayList<Target>();
		Target target2 = new Target();
		target2.setAppId(appId);
		target2.setClientId(cid);
		targets2.add(target);

		String taskId2 = push.getContentId(message2);
		IPushResult ret2 = push.pushMessageToList(taskId2, targets);
	}
	/**
	 * 
	 * 信息模板编辑
	 * 
	 * @return
	 */
	public static NotificationTemplate notificationTemplateDemo(String title, String content) {
		NotificationTemplate template = new NotificationTemplate();
		// 设置APPID与APPKEY
		template.setAppId(appId);
		template.setAppkey(appKey);
		Style0 style = new Style0();
		// 设置通知栏标题与内容
		style.setTitle(title);
		style.setText(content);
		// 配置通知栏图标
		// style.setLogo("C:" + File.s + "jiuan.png");
		// 配置通知栏⽹络图标
		style.setLogoUrl("http://hbtianshou.com/images/images/7638a2c189bd4fa5b5b0d750db6725d4.png");
//		 设置通知是否响铃，震动，或者可清除
		style.setRing(true);
		style.setVibrate(true);
		style.setClearable(true);
		template.setStyle(style);
		// 透传消息设置，1为强制启动应⽤，客户端接收到消息后就会⽴即启动应⽤；2为等待应⽤启动
		template.setTransmissionType(1);
		template.setTransmissionContent(content);
		return template;
	}
}